﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CSmcCs;

namespace Event
{
	/// <summary>
	/// Summary description for Event.
	/// </summary>
	public class Event : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboTargetAxis;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtDeviceName;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.CheckBox chkStop;
		private System.Windows.Forms.CheckBox chkBank;
		private System.Windows.Forms.CheckBox chkOutPulse;
		private System.Windows.Forms.CheckBox chkCountPulse;
		private System.Windows.Forms.TextBox txtBankNo;
		private System.Windows.Forms.TextBox txtOutPulse;
		private System.Windows.Forms.TextBox txtCountPulse;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Label lblComment;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		int			dwRet;
		string		ErrorString;
		short		Id=-1;
		short		AxisNo;
		int			lOutPulse;
		int			lCountPulse;
		string		szText;
		string		szCaption;
		int			wBankNo;
		CSmc		Smc	= new CSmc();

		const int	WM_STOP_EVENT = (int)CSmcConst.CSMC_MESSAGE;
		const int	WM_OUTPULSE_EVENT = (int)CSmcConst.CSMC_MESSAGE+1;
		const int	WM_ENCODER_EVENT = (int)CSmcConst.CSMC_MESSAGE+2;
		const int	WM_BANK_EVENT = (int)CSmcConst.CSMC_MESSAGE+3;

		public Event()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnExit = new System.Windows.Forms.Button();
			this.cboTargetAxis = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtCountPulse = new System.Windows.Forms.TextBox();
			this.txtOutPulse = new System.Windows.Forms.TextBox();
			this.txtBankNo = new System.Windows.Forms.TextBox();
			this.chkCountPulse = new System.Windows.Forms.CheckBox();
			this.chkOutPulse = new System.Windows.Forms.CheckBox();
			this.chkBank = new System.Windows.Forms.CheckBox();
			this.chkStop = new System.Windows.Forms.CheckBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.lblComment = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(272, 352);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(96, 32);
			this.btnExit.TabIndex = 1;
			this.btnExit.Text = "EXIT";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// cboTargetAxis
			// 
			this.cboTargetAxis.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboTargetAxis.Location = new System.Drawing.Point(298, 14);
			this.cboTargetAxis.Name = "cboTargetAxis";
			this.cboTargetAxis.Size = new System.Drawing.Size(70, 23);
			this.cboTargetAxis.Sorted = true;
			this.cboTargetAxis.TabIndex = 3;
			this.cboTargetAxis.SelectedIndexChanged += new System.EventHandler(this.cboTargetAxis_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(240, 18);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(44, 14);
			this.label2.TabIndex = 12;
			this.label2.Text = "AxisNo";
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.AutoSize = false;
			this.txtDeviceName.Location = new System.Drawing.Point(104, 14);
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.Size = new System.Drawing.Size(96, 22);
			this.txtDeviceName.TabIndex = 2;
			this.txtDeviceName.Text = "";
			this.txtDeviceName.TextChanged += new System.EventHandler(this.txtDeviceName_TextChanged);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(9, 18);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(79, 14);
			this.label1.TabIndex = 10;
			this.label1.Text = "Device Name";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtCountPulse,
																					this.txtOutPulse,
																					this.txtBankNo,
																					this.chkCountPulse,
																					this.chkOutPulse,
																					this.chkBank,
																					this.chkStop});
			this.groupBox1.Location = new System.Drawing.Point(9, 48);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(359, 176);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "EVENT";
			// 
			// txtCountPulse
			// 
			this.txtCountPulse.AutoSize = false;
			this.txtCountPulse.Location = new System.Drawing.Point(232, 136);
			this.txtCountPulse.Name = "txtCountPulse";
			this.txtCountPulse.Size = new System.Drawing.Size(112, 22);
			this.txtCountPulse.TabIndex = 14;
			this.txtCountPulse.Text = "";
			this.txtCountPulse.TextChanged += new System.EventHandler(this.txtCountPulse_TextChanged);
			// 
			// txtOutPulse
			// 
			this.txtOutPulse.AutoSize = false;
			this.txtOutPulse.Location = new System.Drawing.Point(232, 96);
			this.txtOutPulse.Name = "txtOutPulse";
			this.txtOutPulse.Size = new System.Drawing.Size(112, 22);
			this.txtOutPulse.TabIndex = 13;
			this.txtOutPulse.Text = "";
			this.txtOutPulse.TextChanged += new System.EventHandler(this.txtOutPulse_TextChanged);
			// 
			// txtBankNo
			// 
			this.txtBankNo.AutoSize = false;
			this.txtBankNo.Location = new System.Drawing.Point(232, 64);
			this.txtBankNo.Name = "txtBankNo";
			this.txtBankNo.Size = new System.Drawing.Size(112, 22);
			this.txtBankNo.TabIndex = 12;
			this.txtBankNo.Text = "";
			// 
			// chkCountPulse
			// 
			this.chkCountPulse.Location = new System.Drawing.Point(8, 136);
			this.chkCountPulse.Name = "chkCountPulse";
			this.chkCountPulse.Size = new System.Drawing.Size(192, 24);
			this.chkCountPulse.TabIndex = 3;
			this.chkCountPulse.Text = "ENCODER COUNT EVENT";
			this.chkCountPulse.CheckedChanged += new System.EventHandler(this.chkCountPulse_CheckedChanged);
			// 
			// chkOutPulse
			// 
			this.chkOutPulse.Location = new System.Drawing.Point(8, 96);
			this.chkOutPulse.Name = "chkOutPulse";
			this.chkOutPulse.Size = new System.Drawing.Size(192, 24);
			this.chkOutPulse.TabIndex = 2;
			this.chkOutPulse.Text = "OUTPULSE COUNT EVENT";
			this.chkOutPulse.CheckedChanged += new System.EventHandler(this.chkOutPulse_CheckedChanged);
			// 
			// chkBank
			// 
			this.chkBank.Location = new System.Drawing.Point(8, 64);
			this.chkBank.Name = "chkBank";
			this.chkBank.Size = new System.Drawing.Size(192, 24);
			this.chkBank.TabIndex = 1;
			this.chkBank.Text = "BANK EVENT";
			this.chkBank.CheckedChanged += new System.EventHandler(this.chkBank_CheckedChanged);
			// 
			// chkStop
			// 
			this.chkStop.Location = new System.Drawing.Point(8, 24);
			this.chkStop.Name = "chkStop";
			this.chkStop.Size = new System.Drawing.Size(192, 24);
			this.chkStop.TabIndex = 0;
			this.chkStop.Text = "STOP EVENT";
			this.chkStop.CheckedChanged += new System.EventHandler(this.chkStop_CheckedChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblComment});
			this.groupBox2.Location = new System.Drawing.Point(9, 232);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(359, 109);
			this.groupBox2.TabIndex = 21;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "COMMENT";
			// 
			// lblComment
			// 
			this.lblComment.Location = new System.Drawing.Point(8, 24);
			this.lblComment.Name = "lblComment";
			this.lblComment.Size = new System.Drawing.Size(344, 73);
			this.lblComment.TabIndex = 0;
			// 
			// Event
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(376, 390);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.groupBox1,
																		  this.btnExit,
																		  this.cboTargetAxis,
																		  this.label2,
																		  this.txtDeviceName,
																		  this.label1});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Event";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Event";
			this.Load += new System.EventHandler(this.Event_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Event());
		}

		private void Event_Load(object sender, System.EventArgs e)
		{
			AxisNo = 1;

			// AxisNo ComboBox
			string	wStr;
			for( int i = 0; i < 8; i++ )
			{
				wStr = string.Format("{0:d}", i+1 );
				cboTargetAxis.Items.Add(wStr);
			}

			// ComboBox Initialise
			cboTargetAxis.SelectedIndex = AxisNo - 1;

			//------------
			// Initialize
			//------------
			txtDeviceName.Text = "SMC000";

			//----------------------
			// Set OutPulse to 500
			//----------------------
			lOutPulse = 500;
			wStr = string.Format("{0:d}", lOutPulse );
			txtOutPulse.Text = wStr;
	
			//---------------------------
			// Set Encoder Pulse to 500
			//---------------------------
			lCountPulse = 500;
			wStr = string.Format("{0:d}", lCountPulse );
			txtCountPulse.Text = wStr;
	
			//---------------------------
			// Set wBankNo to 3
			//---------------------------
			wBankNo = 3;
			wStr = string.Format("{0:d}", wBankNo );
			txtBankNo.Text = wStr;
		}

		private void DispComment(string func, string msg) 
		{
			lblComment.Text = string.Format("{0}: {1}", func, msg );
		}

		private void chkStop_CheckedChanged(object sender, System.EventArgs e)
		{
			byte	bEventMode;
			if( chkStop.Checked == true )
			{
				bEventMode = (byte)CSmcConst.CSMC_ENABLE;
			}
			else
			{
				bEventMode = (byte)CSmcConst.CSMC_DISABLE;
			}
			//----------------------------------
			// Set Event for StopMotion to Driver
			//----------------------------------
			dwRet = Smc.WStopEvent(Id, AxisNo, this.Handle, bEventMode);
			Smc.WGetErrorString(dwRet, out ErrorString);
			DispComment("SmcWStopEvent", ErrorString);
		}

		private void chkBank_CheckedChanged(object sender, System.EventArgs e)
		{
			byte	bEventMode;
			if( chkBank.Checked == true )
			{
				bEventMode = (byte)CSmcConst.CSMC_ENABLE;
			}
			else
			{
				bEventMode = (byte)CSmcConst.CSMC_DISABLE;
			}
			try
			{
				wBankNo = int.Parse(txtBankNo.Text);
			}
			catch (FormatException)
			{
				wBankNo = 0;
			}

			//----------------------------------
			// Set Event for StopMotion to Driver
			//----------------------------------
			dwRet = Smc.WBankEvent(Id, AxisNo, this.Handle, bEventMode, (short)wBankNo);
			Smc.WGetErrorString(dwRet, out ErrorString);
			DispComment("SmcWBankEvent", ErrorString);
		}

		private void chkOutPulse_CheckedChanged(object sender, System.EventArgs e)
		{
			byte	bEventMode;
			try
			{
				lOutPulse = int.Parse(txtOutPulse.Text);
			}
			catch (FormatException)
			{
				lOutPulse = 0;
			}
			if( chkOutPulse.Checked == true )
			{
				bEventMode = (byte)CSmcConst.CSMC_ENABLE;
			}
			else
			{
				bEventMode = (byte)CSmcConst.CSMC_DISABLE;
			}
			//----------------------------------
			// Set Event for OutPulse to Driver
			//----------------------------------
			dwRet = Smc.WCountEvent(Id, AxisNo, this.Handle, bEventMode, (short)CSmcConst.CSMC_OUTPULSE, lOutPulse);
			Smc.WGetErrorString(dwRet, out ErrorString);
			DispComment("SmcWCountEvent", ErrorString);
		}

		private void chkCountPulse_CheckedChanged(object sender, System.EventArgs e)
		{
			byte	bEventMode;
			try
			{
				lCountPulse = int.Parse(txtCountPulse.Text);
			}
			catch (FormatException)
			{
				lCountPulse = 0;
			}

			if( chkCountPulse.Checked == true )
			{
				bEventMode = (byte)CSmcConst.CSMC_ENABLE;
			}
			else
			{
				bEventMode = (byte)CSmcConst.CSMC_DISABLE;
			}

			//----------------------------------
			// Set Event for Encoder to Driver
			//----------------------------------
			dwRet = Smc.WCountEvent(Id, AxisNo, this.Handle, bEventMode, (short)CSmcConst.CSMC_ENCODER, lCountPulse);
			Smc.WGetErrorString(dwRet, out ErrorString);
			DispComment("SmcWCountEvent", ErrorString);
		}

		private void cboTargetAxis_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			AxisNo=(short)(cboTargetAxis.SelectedIndex+1);
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			Smc.WExit(Id);

			this.Close();
		}

		private void txtOutPulse_TextChanged(object sender, System.EventArgs e)
		{
			chkOutPulse_CheckedChanged(chkOutPulse, new System.EventArgs());
		}

		private void txtCountPulse_TextChanged(object sender, System.EventArgs e)
		{
			chkCountPulse_CheckedChanged(chkCountPulse, new System.EventArgs());
		}

		protected override void WndProc(ref Message m)
		{
			switch( m.Msg ) 
			{
				case WM_STOP_EVENT:	// Stop transfer by specified times
					Id	= (short)(m.WParam.ToInt32() & 0x0000ffff);

					szText = string.Format("AxisNo {0:d} was stop.", m.LParam );
					szCaption = "Stop Event";
					MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
					break;
				case WM_BANK_EVENT:		// Transfer stopped
					Id	= (short)(m.WParam.ToInt32() & 0x0000ffff);

					szText = string.Format("BankNo is {0:d}:", wBankNo );
					szCaption = "Bank Event";
					MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
					break;
				case WM_OUTPULSE_EVENT:
					Id	= (short)(m.WParam.ToInt32() & 0x0000ffff);

					szText = string.Format("OutPulse is {0:d}.", lOutPulse);
					szCaption = "OutPulse Event";
					MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
					break;
				case WM_ENCODER_EVENT:
					Id	= (short)(m.WParam.ToInt32() & 0x0000ffff);

					szText = string.Format("Encoder is {0:d}.", lCountPulse);
					szCaption = "Encoder Event";
					MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
					break;
			}
			base.WndProc(ref m);
		}

		private void txtDeviceName_TextChanged(object sender, System.EventArgs e)
		{
			string	wStr="OK";

			if( Id != -1 )
			{
				Smc.WExit(Id);
			}
			wStr = txtDeviceName.Text;
			dwRet = Smc.WInit(wStr, out Id);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWInit", ErrorString);
				Id = -1;
				return ;
			}

			wStr="OK";
			lblComment.Text = wStr;
		}
	}
}
